package com.teacher.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.certificatetask.CertificateGetDto;
import com.teacher.pojo.entity.Certificate;
import com.teacher.mapper.CertificateMapper;
import com.teacher.pojo.enums.items.*;
import com.teacher.pojo.vo.certificate.CertificateGetVo;
import com.teacher.service.ICertificateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 证书申请 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-08-13
 */
@Service
public class CertificateServiceImpl extends ServiceImpl<CertificateMapper, Certificate> implements ICertificateService {

    @Autowired
    private TermMapper termMapper;


    /**
     * 查询证书申请
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<CertificateGetVo>> getPage(CertificateGetDto dto) {
        Page<CertificateGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        IPage<CertificateGetVo> iPage = baseMapper.getPage(page, dto);
        for (CertificateGetVo record : iPage.getRecords()) {
            record.setTypeName(CertificateType.getNameByCode(record.getType()));
            if (record.getType().equals(CertificateType.ENGLISH.getCode())) {
                record.setLevelName(EnglishType.getNameByCode(record.getLevel()));
            } else if (record.getType().equals(CertificateType.SPEAKING.getCode())) {
                record.setLevelName(SpeakType.getNameByCode(record.getLevel()));
            } else if (record.getType().equals(CertificateType.COMPUTER.getCode())) {
                record.setLevelName(ComputerType.getNameByCode(record.getLevel()));
            } else if (record.getType().equals(CertificateType.TEACHER.getCode())) {
                record.setLevelName(TeacherType.getNameByCode(record.getLevel()));
            }
        }
        return CommonResult.success(iPage);
    }
}
